package cn.iocoder.yudao.module.book.service.bookshelf;

import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBooksRespVO;
import cn.iocoder.yudao.module.book.controller.app.bookcase.vo.AppBookshelfRespVO;
import cn.iocoder.yudao.module.book.controller.app.booksShell.vo.AppCollectionsRespVO;
import cn.iocoder.yudao.module.book.controller.app.category.vo.AppzdyReqVO;
import cn.iocoder.yudao.module.book.dal.dataobject.recycleBin.RecycleBinDO;
import cn.iocoder.yudao.module.book.dal.mysql.recycleBin.RecycleBinMapper;
import cn.iocoder.yudao.module.book.service.collections.CollectionsService;
import cn.iocoder.yudao.module.book.service.recycleBin.RecycleBinService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import cn.iocoder.yudao.module.book.controller.admin.bookshelf.vo.*;
import cn.iocoder.yudao.module.book.dal.dataobject.bookshelf.BookshelfDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.book.dal.mysql.bookshelf.BookshelfMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.book.enums.ErrorCodeConstants.*;

/**
 * 书架 Service 实现类
 *
 * @author xi
 */
@Service
@Validated

public class BookshelfServiceImpl implements BookshelfService {

    @Resource
    private BookshelfMapper shelfMapper;

    @Resource
    private RecycleBinMapper recycleBinMapper;

    @Resource
    private CollectionsService collectionsService;

    @Override
    public Integer createshelf(BookshelfSaveReqVO createReqVO) {
        // 插入
        BookshelfDO shelf = BeanUtils.toBean(createReqVO, BookshelfDO.class);
        shelfMapper.insert(shelf);
        // 返回
        return shelf.getBookshelfId();
    }

    @Override
    public void updateshelf(BookshelfSaveReqVO updateReqVO) {
        // 校验存在
        validateshelfExists(updateReqVO.getBookshelfId());
        // 更新
        BookshelfDO updateObj = BeanUtils.toBean(updateReqVO, BookshelfDO.class);
        shelfMapper.updateById(updateObj);
    }

    @Override
    public void deleteshelf(Integer id) {
        // 校验存在
        validateshelfExists(id);
        // 删除
        shelfMapper.deleteById(id);
    }

    private void validateshelfExists(Integer id) {
        if (shelfMapper.selectById(id) == null) {
            throw exception(SHELF_NOT_EXISTS);
        }
    }

    @Override
    public BookshelfDO getshelf(Integer id) {
        return shelfMapper.selectById(id);
    }

    @Override
    public PageResult<BookshelfDO> getshelfPage(BookshelfPageReqVO pageReqVO) {
        return shelfMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AppBookshelfRespVO> getAll(Integer userId) {
        return shelfMapper.getAll(userId);
    }

    @Override
    public Boolean deletes(Long userId, Integer bookId) {
        return shelfMapper.deletes(userId, bookId)>0;
    }

    @Override
    public List<BookshelfDO> selectAll() {
        return shelfMapper.selectList();
    }

    @Override
    public List<AppBooksRespVO> getByCollectionId(Integer id) {
        return shelfMapper.getByCollectionId(id);
    }

    @Override
    public Boolean deleteByBookId(Long userId, Integer collectionId) {
        // 软删除该集合
        boolean ok = shelfMapper.deletes(userId, collectionId) > 0;
        // 回收站记录集合（不是书本）
        RecycleBinDO recycleBinDO = new RecycleBinDO();
        recycleBinDO.setCollectionId(collectionId);
        recycleBinDO.setUserId(userId);
        recycleBinDO.setCategoryId(collectionsService.getCategoryId(collectionId));
        recycleBinDO.setCreateTime(LocalDateTime.now());
        recycleBinDO.setUpdateTime(LocalDateTime.now());
        recycleBinMapper.insert(recycleBinDO);
        return ok;
    }

    @Override
    @Transactional
    public Boolean deleteByCollectionId(Long userId, Integer collectionId) {
        // 直接根据 collectionId 删除指定的集合
        // 1. 软删除书架中的集合记录
        int deletedCount = shelfMapper.deletes(userId, collectionId);

        if (deletedCount > 0) {
            // 2. 添加到回收站
            RecycleBinDO recycleBinDO = new RecycleBinDO();
            recycleBinDO.setCollectionId(collectionId);
            recycleBinDO.setUserId(userId);
            recycleBinDO.setCategoryId(collectionsService.getCategoryId(collectionId));
            recycleBinDO.setCreateTime(LocalDateTime.now());
            recycleBinDO.setUpdateTime(LocalDateTime.now());
            recycleBinMapper.insert(recycleBinDO);
        }

        return deletedCount > 0;
    }

    @Override
    public Boolean markGroupTop(Long userId,Integer id) {
        return shelfMapper.markGroupTop(userId,id)>0;
    }

    @Override
    public Boolean addBook(Integer bookId, Integer collectionId, Integer userId,String time) {
        return shelfMapper.addBook(bookId, collectionId, userId,time);
    }

    @Override
    public List<AppBookshelfRespVO> selectByUserId(Long userId) {
        return shelfMapper.selectByUserId(userId);
    }


    @Override
    public List<BookshelfDO> selectByCategoryId(Long userId, Integer id) {
        return shelfMapper.selectByCategoryId(userId, id);
    }

    @Override
    public List<AppzdyReqVO> selectAllByCategoryIdAndClassifyId(Integer id, Integer classifyId) {
        return shelfMapper.selectAllByCategoryIdAndClassifyId(id,classifyId);
    }
    @Override
    public List<AppCollectionsRespVO> selectAllCollectionsByClassfiId(Integer id) {
        return shelfMapper.selectAllCollectionsByClassfiId(id);
    }

    @Override
    public List<AppBooksRespVO> getdraftByCollectionId(Integer setId) {
        return shelfMapper.getdraftByCollectionId(setId);
    }

}